package com.example.studentmanagersystem;

import com.fs.adapters.ClassesAdapter;
import com.fs.utils.CmdUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class AdminAddClassActivity extends Activity {

	private Button submit;
	private Spinner classSpinner;
	private ArrayAdapter<String> adapter;
	
//	private String className;
	int i=0;
	
	private static final String m[] = { 
		"请选择课程","java", "Android", 
		"Linux","C","PHP",".net","C++",
		"Objective-C","C#","Python","JavaScript",
		"HTML5","FPGA"
		};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.admin_add_class);
		
		this.init();
		
		this.setListener();
	}
	
	/**
	 * initialize widget
	 */
	private void init() {
		
		this.submit=(Button) this.findViewById(R.id.admin_add_class_submit);
		this.classSpinner = (Spinner) this.findViewById(R.id.admin_add_class_spinner);
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, m);
		adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
		classSpinner.setAdapter(adapter);
	}
	
	
	/**
	 * set Listener
	 */
	private void setListener() {
		
		//spinner listener
		this.classSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				 i=position;
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				 
				
			}
		});
		
		//submit listener
		this.submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 
				if(i>0){
					
					//invoking submit method
					addClass(i);
					
				}
				
			}
		});
	}

	
	
	/**
	 * submit method
	 */
	 
	ClassesAdapter  classesAdapter=null;
	 
	public void addClass(int i) {

		showNormalDia(i);
	}
	
	
	 /**
	   * show dialog when submit is clicked
	   */
	  private void showNormalDia(final int i){  
	        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());  
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(this);  
	        normalDia.setIcon(R.drawable.sure);  
	        normalDia.setTitle("提示");  
	        normalDia.setMessage("确定添加？");  
	          
	        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	            	
	            	CmdUtils cmdUtils=new CmdUtils(AdminAddClassActivity.this);
	        		
	        		String cmd="insert into class(class_name) value('"+m[i]+"')";
	        		
	        		System.out.println(m[i]);
	        		 
	        		cmdUtils.submitCmd(URL.CMD_URL,"update",cmd);
	        		
	        		AdminAddClassActivity.this.finish(); 
	                
	            }  
	        });  
	        
	        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            }  
	        });  
	        normalDia.create().show();  
	    }  
	
}
